//
//  HistoryChartViewController.m
//  
//
//  Created by qkong on 15/6/17.
//
//

#import "HistoryChartViewController.h"
@import Charts;
@interface HistoryChartViewController ()

@property (weak, nonatomic) IBOutlet LineChartView *chartView;


@end

@implementation HistoryChartViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.chartView.descriptionText = @"";
    self.chartView.noDataTextDescription = @"没有相关数据";
    
    _chartView.highlightEnabled = NO;
    [_chartView setScaleEnabled:YES];
    _chartView.pinchZoomEnabled = YES;
    _chartView.drawGridBackgroundEnabled = NO;
    _chartView.borderLineWidth = 4.0f;
    _chartView.backgroundColor = [UIColor whiteColor];
    // x-axis limit line
    ChartLimitLine *llXAxis = [[ChartLimitLine alloc] initWithLimit:10.0 label:@"Index 10"];
    llXAxis.lineWidth = 4.0;
    llXAxis.labelPosition = ChartLimitLabelPositionRight;
    llXAxis.valueFont = [UIFont systemFontOfSize:10.f];
    
    [_chartView.xAxis addLimitLine:llXAxis];
    
    ChartYAxis *leftAxis = _chartView.leftAxis;
    [leftAxis removeAllLimitLines];
    leftAxis.customAxisMin = 0.0;
    leftAxis.startAtZeroEnabled = NO;
    leftAxis.gridLineDashLengths = @[@5.f, @5.f];
    leftAxis.drawLimitLinesBehindDataEnabled = YES;
    
    ChartYAxis *rightAxis = _chartView.rightAxis;
    [rightAxis removeAllLimitLines];
    rightAxis.customAxisMin = 0.0;
    rightAxis.startAtZeroEnabled = NO;
    leftAxis.gridLineDashLengths = @[@5.f, @5.f];
    leftAxis.drawLimitLinesBehindDataEnabled = NO;
    
    _chartView.legend.form = ChartLegendFormCircle;
    
    [_chartView animateWithXAxisDuration:2.5 easingOption:ChartEasingOptionEaseInOutQuart];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(getHistory:)
                                                name:HISTORY_DATA_NOTIFICATION_NAME
                                              object:nil];
}

- (void)getHistory:(NSNotification *)notification{
    NSMutableArray *historyData = notification.userInfo[@"history"];
    ChartYAxis *leftAxis = _chartView.leftAxis;
    leftAxis.maxWidth = [[historyData valueForKeyPath:@"@max.intValue"] floatValue];
    ChartYAxis *rightAxis = _chartView.rightAxis;
    rightAxis.maxWidth = [[historyData valueForKeyPath:@"@max.intValue"] floatValue];
    
    
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < 9; i++)
    {
        [xVals addObject:[@(i) stringValue]];
    }
    
    NSMutableArray *yVals = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < historyData.count; i++)
    {
        [yVals addObject:historyData[i]];
    }
    
    NSString *tableName = [NSString stringWithFormat:@"%@历史数据曲线图",self.historyTitle.text];
    LineChartDataSet *set1 = [[LineChartDataSet alloc] initWithYVals:yVals label:tableName];
    
    set1.lineDashLengths = @[@5.f, @2.5f];
    [set1 setColor:[UIColor colorWithRed:0.059 green:0.608 blue:0.263 alpha:1.000]];
    [set1 setCircleColor:[UIColor colorWithRed:0.059 green:0.608 blue:0.263 alpha:1.000]];
    set1.lineWidth = 5.0;
    set1.circleRadius = 5.0;
    set1.drawCircleHoleEnabled = NO;
    
    set1.valueFont = [UIFont systemFontOfSize:9.f];
    set1.fillColor = [UIColor whiteColor];
    
    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
    [dataSets addObject:set1];
    
    LineChartData *data = [[LineChartData alloc] initWithXVals:xVals dataSets:dataSets];
    
    _chartView.data = data;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter]removeObserver:self
                                                   name:HISTORY_DATA_NOTIFICATION_NAME
                                                 object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)backBtnAction:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
